package com.haochang.chunkvod.app.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;

import com.haochang.chunkvod.tools.vod.log.Logger;

import android.os.Environment;

/**
 *@author qintao
 *created at 2016/7/16 10:50
 * 数据库导出工具类
 */

public class DataBaseExportUtil
{
	public static void copy()
	{
		File dbFile = new File(Environment.getDataDirectory() + "/data/com.haochang.chunkvod/databases/order.db");
		//"goodboyenglish_setting.db"为要备份至SD卡上的数据库文件名称
		File file = new File(Environment.getExternalStorageDirectory(), "order.db");
		try
		{
			//创建一个文件，如果文件存在的话会自动把原来的覆盖掉
			file.createNewFile();
			//拷贝文件
			copyFile(dbFile, file);
			Logger.e("成功导出数据库文件至SD卡!");
		} catch (IOException e)
		{
			Logger.e("成功导出数据库文件至SD卡失败!");
		}
	}

	public static void copyFile(File src, File dst) throws IOException
	{
		FileChannel inChannel = new FileInputStream(src).getChannel();
		FileChannel outChannel = new FileOutputStream(dst).getChannel();
		try
		{
			inChannel.transferTo(0, inChannel.size(), outChannel);
		} finally
		{
			if (inChannel != null)
				inChannel.close();
			if (outChannel != null)
				outChannel.close();
		}

	}
}
